package indi.zhifa.engine.core.common.crypto;

import cn.hutool.core.codec.Base64;
import org.apache.commons.codec.digest.MurmurHash3;

import java.nio.ByteBuffer;

public class MurmurHash3Util {
    static public byte[] convertFileFingerprintToByte(long[] pFileFingerprint){
        ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES * pFileFingerprint.length);
        for (long value : pFileFingerprint) {
            buffer.putLong(value);
        }
        byte[] byteArray = buffer.array();
        return byteArray;
    }

    static public String convertFileFingerprintToBase64(long[] pFileFingerprint){
        byte[] byteArray = convertFileFingerprintToByte(pFileFingerprint);
        String base64String = Base64.encode(byteArray);
        return base64String;
    }

    static public String getFileFingerprintBase64(byte[] pBytes){
        byte[] byteArray = getFileFingerprintByte(pBytes);
        // 将字节数组编码为 Base64 字符串
        String base64String = Base64.encode(byteArray);
        return base64String;
    }


    static public long[] getFileFingerprint(byte[] pBytes){
        return MurmurHash3.hash128(pBytes);
    }

    static public byte[] getFileFingerprintByte(byte[] pBytes){
        long[] fileFingerprint = getFileFingerprint(pBytes);
        return convertFileFingerprintToByte(fileFingerprint);
    }

    static public int getShortFingerprint(String pLongStr){
        return MurmurHash3.hash32(pLongStr);
    }

    static public long getLongFingerprint(byte[] pBytes){
        return MurmurHash3.hash32(pBytes);
    }

}
